package com.adcc.swingui.factory;

import java.awt.Container;
import java.lang.reflect.Constructor;
import java.util.Hashtable;

import javax.swing.JDialog;

import org.apache.log4j.Logger;

import com.adcc.swingui.base.DefaultDialog;

/**
 * <p>
 * Title: SwingDialogPool
 * </p>
 * <p>
 * Description: SwingDialog对话框创建工厂
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * 
 * @author jeff.zhao
 * @version 1.0.0
 */
public class SwingDialogFactory {
	private static Logger logger = Logger.getLogger(SwingDialogFactory.class);

	private static Hashtable<String, DefaultDialog> diaHashtable = new Hashtable<String, DefaultDialog>();

	public static void removeDialog(String dialogType) {
		diaHashtable.remove(dialogType);
	}
	
	public static DefaultDialog create(String className, boolean usePool, Container container) {
		DefaultDialog dia = null;
		if(usePool) {
			if (diaHashtable.containsKey(className)) {
				dia = diaHashtable.get(className);
				dia.requestFocus();
			} else {
				try {
					Class c = Class.forName(className);
					Constructor con=c.getConstructor(new Class[]{Container.class});
					dia = (DefaultDialog)con.newInstance(container);
				} catch (Exception e) {
					logger.error("工具对话框失败" + className + e.getMessage());
				} 
				dia.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
				diaHashtable.put(dia.getClass().getName(), dia);
			}
		} else {
			try {
				Class c = Class.forName(className);
				Constructor con=c.getConstructor(new Class[]{Container.class});
				dia = (DefaultDialog)con.newInstance(container);
			} catch (Exception e) {
				logger.error("工具对话框失败" + className + e.getMessage());
			} 
			dia.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		}
		return dia;
	}
}
